// Created by WXX on 2021/10/22 20:14
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int lengthOfLastWord(string s) {

        for (int i = s.size() - 1; i >= 0; i--) {
            if (s[i] == ' ') continue;
            int j = i;
            while (j >= 0 && s[j] != ' ') j--;
            return i - j;
        }
        return 0;
    }
};

int main() {

    cout << Solution().lengthOfLastWord("   fly me   to   the moon  ") << endl;  // 4

    return 0;
}
